/*
===============================================================================
                    EntitySpaces Studio by EntitySpaces, LLC
             Persistence Layer and Business Objects for Microsoft .NET
             EntitySpaces(TM) is a legal trademark of EntitySpaces, LLC
                          http://www.entityspaces.net
===============================================================================
EntitySpaces Version : 2012.1.0930.0
EntitySpaces Driver  : SQL
===============================================================================
*/

using System;
using System.Collections.Generic;
using System.Linq;
using EntitySpaces.Core;
using EntitySpaces.Interfaces;
using EntitySpaces.DynamicQuery;

namespace DNNspot.PropertyAgentImport
{
	public partial class Property : esProperty
	{
		public Property()
		{
		
		}
        public static bool DeleteByModuleId(int moduleId)
        {
            bool status = false;
            using (esTransactionScope transaction = new esTransactionScope())
            {
                PropertyQuery q = new PropertyQuery();
                q.Where(q.ModuleID == moduleId);

                PropertyCollection propertiesToDelete = new PropertyCollection();
                propertiesToDelete.Load(q);
                IEnumerable<int> propertyIdsToDelete = propertiesToDelete.Select(x => x.PropertyID.Value); //.ToArray();

                PropertyValueQuery vq = new PropertyValueQuery();
                vq.Where(vq.PropertyID.In(propertyIdsToDelete));

                PropertyValueCollection propertyValues = new PropertyValueCollection();
                propertyValues.Load(vq);

                propertyValues.MarkAllAsDeleted();
                propertyValues.Save();

                propertiesToDelete.MarkAllAsDeleted();
                propertiesToDelete.Save();

                transaction.Complete();
                status = true;
            }
            return status;
        }

	}
}
